package com.hq.fxh.No3_TuiGuang.MyMoBan.Activity;

import android.app.Activity;
import android.content.DialogInterface;
import android.content.Intent;
import android.support.v7.app.AlertDialog;
import android.support.v7.widget.RecyclerView;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ImageView;

import com.hq.fxh.HttpApi.UserServiceImpl;
import com.hq.fxh.Intefers.NetworkSubscriber;
import com.hq.fxh.Modle.AdMaterialCommonBean;
import com.hq.fxh.No3_TuiGuang.MyMoBan.Activity.item.ToastUtils;
import com.hq.fxh.No3_TuiGuang.MyMoBan.Config;
import com.hq.fxh.No3_TuiGuang.MyMoBan.Model.CommonRes;
import com.hq.fxh.R;
import com.hq.fxh.Utils.GlideUtils;

import java.util.List;


/**
 * @author 小谢
 * @ClassName AdMaterialAdapter
 * @Description :
 * @Created 2017/1/17 0017
 */
public class AdMaterialAdapter extends RecyclerView.Adapter<AdMaterialAdapter.MyViewHolder> {
    private List<AdMaterialCommonBean> mDatas;
    private Activity mContext;
    private int uid;
    private LayoutInflater inflater;
    private boolean isdelete;

    public AdMaterialAdapter(Activity context, List<AdMaterialCommonBean> datas, int uid, boolean isdelete) {
        this.mContext = context;
        this.mDatas = datas;
        this.uid = uid;
        this.isdelete = isdelete;
        inflater = LayoutInflater.from(mContext);
    }

    @Override
    public AdMaterialAdapter.MyViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {
        View view = inflater.inflate(R.layout.item_ad_material, parent, false);
        MyViewHolder holder = new MyViewHolder(view);
        return holder;
    }

    @Override
    public void onBindViewHolder(AdMaterialAdapter.MyViewHolder holder, final int position) {
//        IImageLoader.getInstance().load(mContext,holder.img, mDatas.get(position).getMaterial_url());
        GlideUtils.loadImage(mContext, mDatas.get(position).getMaterial_url(), holder.img);

        holder.view.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                Intent intent = new Intent();
                intent.putExtra(Config.URL, mDatas.get(position).getMaterial_url());
                mContext.setResult(Config.ResultCode.TEMPLETE_CHOOSE, intent);
                mContext.finish();
            }
        });
        if (isdelete) {
            holder.view.setOnLongClickListener(new View.OnLongClickListener() {
                @Override
                public boolean onLongClick(View v) {
                    AlertDialog.Builder builder = new AlertDialog.Builder(mContext);
                    CharSequence[] strs = new CharSequence[]{"删除"};
                    builder.setItems(strs, new DialogInterface.OnClickListener() {
                        @Override
                        public void onClick(DialogInterface dialog, int which) {
                            delMemberMaterial(mDatas.get(position));
                        }
                    });
                    builder.show();
                    return false;
                }
            });
        }
    }

    private void delMemberMaterial(final AdMaterialCommonBean adData) {
        UserServiceImpl.delMemberMaterial(uid, adData.getMaterial_id(), new NetworkSubscriber<CommonRes>() {
            @Override
            public int onSuccess(CommonRes data) {
                super.onSuccess(data);
                if (data != null && data.getCode() == 0) {
                    removeItem(adData);
                    notifyDataSetChanged();
                } else {
                    ToastUtils.show(mContext, data.getMsg());
                }
                return 0;
            }
        });
    }

    public void addItem(AdMaterialCommonBean bean, int position) {//object 数据类型
        mDatas.add(position, bean);
        notifyItemInserted(position); //Attention!
    }

    @Override
    public int getItemCount() {
        return mDatas != null ? mDatas.size() : 0;
    }

    public boolean removeItem(AdMaterialCommonBean bean) {
        int position = mDatas.indexOf(bean);
        if (position >= 0) {
            mDatas.remove(position);
            notifyItemRemoved(position);//Attention!
            return true;
        } else {
            return false;
        }
    }

    public class MyViewHolder extends RecyclerView.ViewHolder {
        ImageView img;
        public View view;

        public MyViewHolder(View view) {
            super(view);
            this.view = view;
            img = (ImageView) view.findViewById(R.id.ad_img);
        }
    }

}
